package com.ciwong.epaper.modules.me.ui;

import android.content.pm.PackageManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.me.util.MeJumpManager;
import com.ciwong.libs.utils.CWSys;
import com.ciwong.libs.utils.NetworkUtils;
import com.ciwong.mobilelib.i.ViewOnClickListener;
import com.ciwong.mobilelib.ui.BaseActivity;
import com.ciwong.mobilelib.utils.BaseSystem;
import com.ciwong.mobilelib.utils.CWUpdate;
import com.ciwong.mobilelib.utils.EAction;
import com.ciwong.mobilelib.utils.ToastUtil;

/**
 * 关于我们
 */
public class AboutUsActivity extends BaseActivity {

    private TextView app_version;

    private ImageView img_update_tips;

    private RelativeLayout ly_version_update;

    private RelativeLayout about_agreement_shiyong;

    private RelativeLayout about_agreement_yingshi;

    private RelativeLayout about_agreement_ertong;

    private RelativeLayout about_agreement_zaiyao;

    private RelativeLayout about_agreement_grqd;

    private RelativeLayout about_agreement_sdkqd;

    private RelativeLayout about_agreement_qxqd;


    private String title;

    @Override
    protected int setView() {
        return R.layout.activity_about_us;
    }

    @Override
    protected void findViews() {
        app_version = findViewById(R.id.app_version);
        img_update_tips = findViewById(R.id.img_update_tips);
        ly_version_update = findViewById(R.id.ly_version_update);
        about_agreement_shiyong = findViewById(R.id.about_agreement_shiyong);
        about_agreement_yingshi = findViewById(R.id.about_agreement_yingshi);
        about_agreement_ertong = findViewById(R.id.about_agreement_ertong);
        about_agreement_zaiyao = findViewById(R.id.about_agreement_zaiyao);
        about_agreement_grqd = findViewById(R.id.about_agreement_grqd);
        about_agreement_sdkqd = findViewById(R.id.about_agreement_sdkqd);
        about_agreement_qxqd = findViewById(R.id.about_agreement_qxqd);
    }


    @Override
    protected void initEvent() {
        about_agreement_shiyong.setOnClickListener(mViewOnClickListener);
        about_agreement_yingshi.setOnClickListener(mViewOnClickListener);
        about_agreement_ertong.setOnClickListener(mViewOnClickListener);
        about_agreement_zaiyao.setOnClickListener(mViewOnClickListener);
        about_agreement_grqd.setOnClickListener(mViewOnClickListener);
        about_agreement_sdkqd.setOnClickListener(mViewOnClickListener);
        about_agreement_qxqd.setOnClickListener(mViewOnClickListener);
    }

    @Override
    protected void init() {
        setValideSource(false);
        showBackBar();
        title = getString(R.string.about_us);
        setTitleText(title);
        try {
            app_version.setText(getPackageManager()
                    .getPackageInfo(getPackageName(), 0).versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        long versionCode = CWSys.getSharedInt(CWUpdate.APK_UPDATE_SERVICE_VERSION_CODE, -1);
        int sysVersionCode = BaseSystem.getSystemVersionCode(getApplicationContext());

        if (versionCode > 0 && sysVersionCode < versionCode) {
            img_update_tips.setVisibility(View.VISIBLE);
        } else {
            img_update_tips.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    protected void loadData() {
        ly_version_update.setOnClickListener(mViewOnClickListener);
    }

    private ViewOnClickListener mViewOnClickListener = new ViewOnClickListener() {
        @Override
        public void avertRepeatOnClick(View v) {
            int id = v.getId();
            if (id == R.id.ly_version_update) {
                upData();
            } else if (id == R.id.about_agreement_shiyong) {
                //习习教师使用协议
                MeJumpManager.jumpAgreementActivity(R.string.go_back, AboutUsActivity.this, getString(R.string.agreement_use_url), getString(R.string.agreement_private), AgreementActivity.LOGIN_ACTION);
            } else if (id == R.id.about_agreement_yingshi) {
                //习习教师隐私政策
                MeJumpManager.jumpAgreementActivity(R.string.go_back, AboutUsActivity.this, getString(R.string.agreement_secret_url), getString(R.string.agreement_secret), AgreementActivity.LOGIN_ACTION);
            } else if (id == R.id.about_agreement_ertong) {
                //儿童隐私保护政策
                MeJumpManager.jumpAgreementActivity(R.string.go_back, AboutUsActivity.this, getString(R.string.agreement_child_url), getString(R.string.agreement_child), AgreementActivity.LOGIN_ACTION);
            } else if (id == R.id.about_agreement_zaiyao) {
                //习习教师隐私政策摘要
                MeJumpManager.jumpAgreementActivity(R.string.go_back, AboutUsActivity.this, getString(R.string.agreement_abstract_url), getString(R.string.agreement_abstract), AgreementActivity.LOGIN_ACTION);
            } else if (id == R.id.about_agreement_grqd) {
                //习习教师个人信息收集清单
                MeJumpManager.jumpAgreementActivity(R.string.go_back, AboutUsActivity.this, getString(R.string.agreement_collect_url), getString(R.string.agreement_collect_list), AgreementActivity.LOGIN_ACTION);
            } else if (id == R.id.about_agreement_sdkqd) {
                //习习教师第三方共享与SDK清单
                MeJumpManager.jumpAgreementActivity(R.string.go_back, AboutUsActivity.this, getString(R.string.agreement_sdk_list_url), getString(R.string.agreement_sdk_list), AgreementActivity.LOGIN_ACTION);
            } else if (id == R.id.about_agreement_qxqd) {
                //习习教师设备权限清单
                MeJumpManager.jumpAgreementActivity(R.string.go_back, AboutUsActivity.this, getString(R.string.agreement_device_perm_url), getString(R.string.agreement_device_perm_list), AgreementActivity.LOGIN_ACTION);
            }
        }
    };

    /**
     * 更新APK
     */
    private void upData() {

        if (!NetworkUtils.isOnline()) {
            ToastUtil.INSTANCE.toastCenterNoNetError();
            return;
        }

        if (!isShowMiddleProgressBar()) {
            showProgressLoading();
            CWUpdate update = new CWUpdate(this, EAction.getUpgradeUrl(),
                    new CWUpdate.Callback() {

                        @Override
                        public void excuteSelfMethod(Object... obj) {
                            dismissProgress();
                            if (obj != null && obj.length > 0) {
                                if (obj[0].equals(CWUpdate.UPDATE_FLAG_ERROR)) {
                                    showToastError(
                                            R.string.download_apk_failed);
                                } else if (obj[0]
                                        .equals(CWUpdate.UPDATE_FLAG_ISLATEST)) {
//                                    showToastSuccess(R.string.alreadylatest);
                                    ToastUtil.INSTANCE.toastCenter(EApplication.getInstance(), R.string.alreadylatest, R.mipmap.ic_new_version);
                                } else if (obj[0]
                                        .equals(CWUpdate.UPDATE_BACKGROUND)) {
                                } else if (obj[0].equals(CWUpdate.COMPLETE)) {
                                }
                            }
                        }
                    });
            update.excute(false);
        }
    }

    private void showProgressLoading() {
        showMiddleProgressBar(title);
    }

    private void dismissProgress() {
        hideMiddleProgressBar();
    }

}
